﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Z1029.NoteEditor
{
    public class ViewCtrl : MonoBehaviour
    {
        public Camera cam;
        public RectTransform re;
        public GameObject mouse;
        public Vector3 lastMousePos;
        public bool isMoving;
        public static ViewType viewType;
        public UtilsBtn[] selectTypeBtns;
        public enum ViewType
        {
            Hand, Move
        }

        private void Start()
        {
            viewType = ViewType.Move;
            cam = LevelController.instance.viewCam;
            cam.transform.position = new Vector3(0, 0, -8);
        }

        void Update()
        {
            lastMousePos = mouse.transform.position;
            if (true)
            {
                float size = cam.orthographicSize / 5;
                mouse.transform.localScale = new Vector3(size, size, 0.0000001f);
                mouse.transform.position = new Vector3(cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - RectToMouse(re).x, Input.mousePosition.y - RectToMouse(re).y, LevelController.instance.camDistance)).x, cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - RectToMouse(re).x, Input.mousePosition.y - RectToMouse(re).y, LevelController.instance.camDistance)).y, -1);
            }
            if (Input.GetKey(KeyCode.LeftControl) && MouseInView())
            {
                float delta = Input.GetAxis("Mouse ScrollWheel");
                cam.orthographicSize -= delta * 2;
            }
            else if (MouseInView())
            {
                LevelController.instance.NowBeatTime += (int)(Input.GetAxis("Mouse ScrollWheel") * 50);
            }

            if (MouseInView())
            {
                if (!Input.GetKey(KeyCode.LeftControl))
                {
                    Vector2 deltaCamMove = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
                    if (!Input.GetKey(KeyCode.LeftShift))
                    {
                        deltaCamMove /= 3;
                    }
                    cam.transform.Translate(new Vector3(deltaCamMove.x, deltaCamMove.y, 0));
                }
            }

            if (Input.GetKeyDown(KeyCode.Alpha0) && MouseInView())
            {
                cam.transform.position = new Vector3(0, 0, -8);
            }

            for (int i = 0; i < selectTypeBtns.Length; i++)
            {
                if (selectTypeBtns[i].select)
                {
                    viewType = (ViewType)i;
                    break;
                }
            }

            //if(viewType == ViewType.Hand)
            //{
            //    if (Input.GetMouseButton(0))
            //    {
            //        isMoving = true;
            //        Vector3 delta = mouse.transform.position - lastMousePos;
            //        delta.z = 0;
            //        //delta.x = cam.ScreenToWorldPoint(new Vector3()).y;
            //        cam.transform.Translate(delta);
            //        mouse.transform.position = lastMousePos;
            //    }
            //    else
            //        isMoving = false;
            //}

        }

        public bool MouseInView()
        {
            return Input.mousePosition.x - RectToMouse(re).x < re.sizeDelta.x && Input.mousePosition.x - RectToMouse(re).x > 0;
        }

        public Vector2 RectToMouse(RectTransform rect)
        {
            Vector2 res = Vector2.zero;
            res.x = rect.anchoredPosition.x + Screen.width / 2 - rect.sizeDelta.x / 2;
            res.y = rect.anchoredPosition.y + Screen.height / 2 - rect.sizeDelta.y / 2;
            return res;
        }
    }
}